import { type RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const Layouts = () => import("@/layouts/index.vue")

/** 常驻路由 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/userManage/index",
    //  用户管理
    name: "userManage",
    meta: {
      hidden: true
    },
    component: () => import("@/views/userManage/index.vue"),
    children: [
      {
        path: "index",
        component: () => import("@/views/userManage/index.vue"),
        name: "userManage",
        meta: {
          title: "",
          hidden: true,
          svgIcon: ""
        }
      }
    ]
  },
  // 运行详情页面，就是训练完点击跳转的详情页
  {
    path: "/runDetails/index",
    name: "runDetails",
    component: () => import("@/views/runDetails/index.vue"),
    meta: {
      hidden: true
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/runDetails/index.vue"),
        name: "runDetails",
        meta: {
          title: "",
          hidden: true,
          svgIcon: ""
        }
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    redirect: "/login",
    name: "/",
    meta: {
      hidden: true
    }
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/index/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/register",
    name: "register",
    component: () => import("@/views/register/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/comments",
    name: "comments",
    component: () => import("@/views/comment/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/find",
    name: "find",
    component: () => import("@/views/register/find.vue"),
    meta: {
      hidden: true
    }
  },
  //数据管理
  {
    path: "/dataManage",
    component: Layouts,
    redirect: "/dataManage/index",
    meta: {
      hidden: false
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/dataManage/index.vue"),
        name: "dataManage",
        meta: {
          title: "数据管理",
          svgIcon: "dataManage",
          hidden: false
        }
      }
    ]
  },
  // train 训练页面
  {
    path: "/modelTrain",
    component: Layouts,
    meta: {
      hidden: false
    },
    redirect: "/modelTrain/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/dashboard/index.vue"),
        name: "modelTrain",
        meta: {
          title: "模型训练",
          svgIcon: "modelTrain",
          affix: true,
          hidden: false
        }
      }
    ]
  },
  //manage 模型管理
  {
    path: "/modelManage",
    component: Layouts,
    redirect: "/modelManage/index",
    meta: {
      hidden: false
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/modelManage/index.vue"),
        name: "modelManage",
        meta: {
          title: "模型管理",
          svgIcon: "modelManage",
          hidden: false
        }
      }
    ]
  },
  //predict 预测页面
  {
    path: "/modelPredict",
    component: Layouts,
    redirect: "/modelPredict/index",
    name: "modelPredict",
    meta: {
      title: "模型预测",
      svgIcon: "modelPredict",
      hidden: false
    },
    children: [
      {
        path: "grid",
        component: () => import("@/views/modelPredict/index.vue"),
        name: "grid",
        meta: {
          title: "网格预测",
          hidden: false
        }
      },
      {
        path: "excel",
        component: () => import("@/views/modelPredict/index_excel.vue"),
        name: "excel",
        meta: {
          title: "excel文件预测",
          hidden: false
        }
      }
    ]
  },
  // 文件管理
  {
    path: "/fileConfig",
    component: Layouts,
    redirect: "/fileConfig/index",
    meta: {
      hidden: true
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/fileConfig/index.vue"),
        name: "fileConfig",
        meta: {
          title: "文件管理",
          svgIcon: "fileConfig",
          hidden: false
        }
      }
    ]
  },
  {
    path: "/auth",
    component: Layouts,
    redirect: "/auth/index",
    meta: {
      hidden: true
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/auth/index.vue"),
        name: "auth",
        meta: {
          title: "授权管理",
          svgIcon: "auth",
          hidden: false
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = []

const router = createRouter({
  history:
    import.meta.env.VITE_ROUTER_HISTORY === "hash"
      ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
      : createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
